package cn.showw.uitl;

import java.io.*;

public class AixKeypair {
	
	public static String ROOT_PATH = "/sdcard/";

	//static {
	//	ROOT_PATH = getRootPath();
	//}

	//private static String getRootPath() {
	//	return Environment.getExternalStorageDirectory().getPath();
	//}

	private static File createNewFile(String path) throws IOException {
		File file = new File(path);
		if (!file.exists()) {
			file.getParentFile().mkdirs();
			file.createNewFile();
		}
		return file;
	}

	public static boolean deleteFile(String path) {
		File file = new File(ROOT_PATH + path);
		if (file.exists()) {
			return file.delete();
		} else {
			return true;
		}
	}

	public static boolean put(String path, String key, Object value) {
		try {
			String text = "<" + key + ">" + value + "</" + key + ">\r\n";
			File file = createNewFile(ROOT_PATH + path);
			BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true), "UTF-8"));
			if (exist(path, key)) {
				return replace(path, key, text);
			} else {
				bw.write(text);
				bw.flush();
				bw.close();
				return true;
			}
		} catch (IOException e) {
			return false;
		}
	}

	public static String get(String path, String key, Object value) {
		String obj = get(path, key);
		if (obj == null) {
			return value.toString();
		} else {
			return obj.toString();
		}
	}

	private static String get(String path, String key) {
		try {
			BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(createNewFile(ROOT_PATH + path)), "UTF-8"));
			while (true) {
				String line = br.readLine();
				if (line == null || line.equals("")) {
					br.close();
					return null;
				} else {
					if (line.matches("<" + key + ">.*?</" + key + ">")) {
						String iovalue = line.replaceAll("<" + key + ">(.*?)</" + key + ">", "$1");
						return iovalue;
					}
				}
			}
		} catch (IOException e) {
			return null;
		}
	}

	public static boolean exist(String path, String key) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(createNewFile(ROOT_PATH + path)), "UTF-8"));
		while (true) {
			String line = br.readLine();
			if (line == null || line.equals("")) {
				br.close();
				return false;
			} else {
				if (line.matches("<" + key + ">.*?</" + key + ">")) {
					return true;
				}
			}
		}
	}

	public static boolean replace(String file, String key, String text) throws IOException {
		String path = ROOT_PATH + "/temp_313198647.txt";
		BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(createNewFile(ROOT_PATH + file)), "UTF-8"));
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(createNewFile(path)), "UTF-8"));
		String regex = "<" + key + ">.*?</" + key + ">";
		String line = "";
		while ((line = br.readLine()) != null) {
			bw.write(line.replaceAll(regex, text) + "\r\n");
		}
		bw.flush();
		bw.close();
		br.close();
		BufferedReader br1 = new BufferedReader(new InputStreamReader(new FileInputStream(createNewFile(path)), "UTF-8"));
		BufferedWriter bw1 = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(createNewFile(ROOT_PATH + file)), "UTF-8"));
		String line1 = null;
		while ((line1 = br1.readLine()) != null) {
			if (!line1.equals("")) 
				bw1.write(line1 + "\r\n");
		}
		bw1.flush();
		bw1.close();
		br1.close();
		return deleteFile(path);
	}
	
}
